package com.lanyou.cook.camel.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;

import com.lanyou.cook.camel.entity.FtpServer;
import com.lanyou.cook.camel.util.ExceptionToStrFactory;


public class FtpServerDao extends BaseDao<FtpServer>{
	
	private static Logger logger = Logger.getLogger(FtpServerDao.class);
	RowMapper<FtpServer> mapper = new RowMapper<FtpServer>() {
		public FtpServer mapRow(ResultSet rs, int rowNum) throws SQLException {
			FtpServer rcd = new FtpServer();
			rcd.setId(rs.getString("IF_FTP_SERVER_ID"));
			rcd.setFtpId(rs.getString("IF_FTP_ID"));
			rcd.setHost(rs.getString("HOST"));
			rcd.setPort(rs.getString("PORT"));
			rcd.setUserName(rs.getString("USER_NAME"));
			rcd.setPassword(rs.getString("PASSWORD"));
			rcd.setIsAnonymous(rs.getString("IS_ANONYMOUS"));
			rcd.setIsDelSourceFile(rs.getString("IS_DEL_SOURCE"));
			rcd.setIsCoverDesFile(rs.getString("IS_COVER"));
			rcd.setFileDir(rs.getString("FILE_DIR"));
			rcd.setHostType(rs.getString("HOST_TYPE"));
			rcd.setIsEnable(rs.getString("IS_ENABLE"));
			rcd.setControlEncoding(rs.getString("CONTROL_ENCODING"));
			rcd.setCreator(rs.getString("CREATOR"));
			rcd.setCreatedDate(rs.getDate("CREATED_DATE"));
			rcd.setModifier(rs.getString("MODIFIER"));
			rcd.setLastUpdateDate(rs.getDate("LAST_UPDATED_DATE"));
			rcd.setFileName(rs.getString("FILE_NAME"));
			rcd.setPassiveMode(rs.getString("PASSIVE_MODE"));
			return rcd;
		}
	};
	
	public List<FtpServer> findFtpServersByFtpId(String ftpId) {
		logger.info("[FtpServerDao.findFtpServersByFtpId]:ftpId="+ftpId);
		if(StringUtils.isNotBlank(ftpId));
		String sql = "SELECT * FROM t_esb_bu_if_ftp_server WHERE IS_ENABLE='1' and if_ftp_id = ?";
		List<FtpServer> record = this.getJdbcTemplate().query(sql, mapper, ftpId);
		return record;
	};
	
	
	public FtpServer findFtpServersById(String id) {
//		logger.info("[FtpServerDao.findFtpServersById]:Id="+id);
		if(StringUtils.isNotBlank(id));
		String sql = "SELECT * FROM t_esb_bu_if_ftp_server WHERE IS_ENABLE = '1' and IF_FTP_SERVER_ID = ?";
		FtpServer ftpServer = new FtpServer();
		try{
			ftpServer = this.getJdbcTemplate().queryForObject(sql, mapper, id);
			return ftpServer;
		}catch(DataAccessException e){
			logger.error("[FtpServerDao.findFtpServersById]:Id="+id+"---"+ExceptionToStrFactory.getErrorInfoFromException(e));
			return null;
		}
	};
}
